import {
  get,
  searchVal
} from '../../utils/db'
const db = wx.cloud.database()

Page({
  data: {
    lists: [],
  },

  //分类列表
  async onLoad(e) {
    //搜索传来的是name
    if (e.name) {
      var result = await searchVal("menu", {
        name: db.RegExp({
          regexp: e.name
        })
      })
    } else {
      //传的是id
      var _id = e.id;
      var result = await get("menu", {
        classId: _id
      });
    }
    //星星
    var lists = result.data;
    lists.forEach(item => {
      if (item.views == 0) {
        item.star = 0;
      } else if (item.views > 0 && item.views <= 10) {
        item.star = 1;
      } else if (item.views > 10 && item.views <= 20) {
        item.star = 2;
      } else if (item.views > 20 && item.views <= 30) {
        item.star = 3;
      } else if (item.views > 30 && item.views <= 40) {
        item.star = 4;
      } else {
        item.star = 5;
      }
    })
    this.setData({
      lists
    })
  },
  //跳转到详情页面
  async detail(e) {
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '../detail/detail?id=' + id,
    })
  }
})